package com.gtfrank.web.servlet.day3;

import com.gtfrank.web.servlet.pojo.User;
import com.gtfrank.web.servlet.service.IUserService;
import com.gtfrank.web.servlet.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

/**
 * @ProjectName: sudacoursetrainging
 * @ClassName: SerachUserServlet
 * @PackageName:com.gtfrank.web.servlet.day3
 * @Description: 根据用户名来查询
 * @Author: frank
 * @Date: 2021/4/29 15:13
 * @Version v1.0
 */
@WebServlet(value = "/user/SerachUserServlet")
public class SerachUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            try {
                resp.setContentType("text/html");
                resp.setCharacterEncoding("utf-8");
                PrintWriter out = resp.getWriter();
                String name  = req.getParameter("userName");
                IUserService userService = new UserServiceImpl();
                User user = userService.findUserByName(name);
                out.println("<center> <h1>单个用户的详细信息</h1></center>");
                out.println(" <hr>");
                out.println("    <table border=\"1\" bgcolor=\"#7fffd4\" width=\"800px\" align=\"center\">");
                out.println("        <tr>");
                out.println("            <td>ID</td>");
                out.println("            <td>用户名</td>");
                out.println("            <td>密码</td>");
                out.println("            <td>操作</td>");
                out.println("        </tr>");

                    out.println("        <tr>");
                    out.println("            <td>"+user.getId()+"</td>");
                    out.println("            <td>"+user.getName()+"</td>");
                    out.println("            <td>"+user.getPassword()+"</td>");
                    out.println("            <td><a href=\"/javaeeproject/user/showUserListServlet\">返回</a>");
                    out.println("        </tr>");

                out.println("");
                out.println("    </table>");
                out.println("</body>");


                out.flush();
                out.close();
            }catch (Exception e){
                e.printStackTrace();
                req.getRequestDispatcher("/user/showUserListServlet").forward(req,resp);
            }
    }
}
